package com.java.zhuiyun.api.feedback.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.zhuiyun.api.feedback.entity.Feedback;
import com.java.zhuiyun.api.feedback.mapper.FeedbackMapper;
import com.java.zhuiyun.api.feedback.service.IFeedbackService;
import com.java.zhuiyun.api.SysUser.common.Result;
import com.java.zhuiyun.api.SysUser.common.RoleEnum;
import com.java.zhuiyun.api.SysUser.entity.SysUser;
import com.java.zhuiyun.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @ClassName FeedbackController
 * @Description TODO 反馈管理 用于uniapp中用户反馈
 * @Author xy
 * @Date 2023/9/14 10:51
 **/
@RestController
@Api(tags = "反馈管理")
@RequestMapping("/feedback")
@Slf4j
public class FeedbackController {

    @Resource
    private IFeedbackService feedbackService;

    @Resource
    private FeedbackMapper feedbackMapper;

    /**
    * @Author xy
    * @Description TODO 反馈管理的分页查询筛选 用于uniapp中用户反馈
    * @Date 2023/9/14 11:09
    * @Return Result 返回类型
    * @param name 反馈人姓名
    * @param pageNum 当前页
    * @param pageSize 每页显示条数
    */
    @GetMapping("/page")
    @ApiOperation(value = "查询")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        SysUser currentUser = TokenUtils.getCurrentUser();
        // 角色是普通用户
        if (RoleEnum.ROLE_USER.toString().equals(currentUser.getRole())) {
            queryWrapper.eq("user", currentUser.getUserName());
        }
        Page<Feedback> page = feedbackService.page(new Page<>(pageNum, pageSize), queryWrapper);
        //分割图片,
        for (Feedback record : page.getRecords()) {
            String[] imgArray = record.getImg().split(",");
            // 检查是否存在结尾的逗号并删除
            if (imgArray.length > 0 && imgArray[imgArray.length - 1].isEmpty()) {
                // 删除结尾的逗号
                imgArray = Arrays.copyOf(imgArray, imgArray.length - 1);
            }
            // 设置处理后的图片数组
            record.setLogdimg(imgArray);
        }
        return Result.success(page);
    }

    /**
    * @Author xy
    * @Description TODO 反馈管理的审核成功 （后台管理）
    * @Date 2023/9/14 11:08
    * @Return Result 返回类型
    * @param feedback 反馈实体类
    * @param file 图片
    */
    @PostMapping("/save")
    @ApiOperation(value = "修改")
    public Result save(@RequestBody Feedback feedback, MultipartFile file) {
        feedbackService.FeedbackSucceed(feedback, file);
        return Result.success();
    }

    /**
     * @Author xy
     * @Description TODO 反馈管理的审核失败 （后台管理）
     * @Date 2023/9/14 11:08
     * @Return Result 返回类型
     * @param feedback 反馈实体类
     * @param file 图片
     */
    @PostMapping("/Fail")
    @ApiOperation(value = "修改")
    public Result FeedbackFail(@RequestBody Feedback feedback, MultipartFile file) {
        feedbackService.FeedbackFail(feedback, file);
        return Result.success();
    }

    /**
    * @Author xy
    * @Description TODO 反馈管理的逻辑删除接口 （后台管理）
    * @Date 2023/9/14 11:13
    * @Return Result 返回类型
    * @param id 反馈id
    */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public Result delete(@PathVariable Integer id) {
        Feedback feedback = feedbackMapper.selectById(id);
        feedback.setDelFlag(true);
        feedbackMapper.deleteById(id);
        return Result.success();
    }

}
